package com.njsoft.bodyawakening.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.njsoft.bodyawakening.BodyApplication;
import com.njsoft.bodyawakening.utils.L;

/**
 * Created by KID on 2017/11/14.
 * 随意拖动的view
 */

public class DragView extends android.support.v7.widget.AppCompatTextView {

    private int width;
    private int height;
    private int screenWidth;
    private int screenHeight;
    private Context context;

    //是否拖动
    private boolean isDrag = false;
    private String TAG = "dragView-->";

    public boolean isDrag() {
        return isDrag;
    }

    public DragView(Context context) {
        super(context);
        this.context = context;
    }

    public DragView(Activity context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取自身的宽度
        width = getMeasuredWidth();
        //获取自身的高度
        height = getMeasuredHeight();
        //屏幕的宽度
        screenWidth = BodyApplication.GRIDVIEW_WIDTH;
        //屏幕的高度
        screenHeight = BodyApplication.GRIDVIEW_HEIGHT;

    }

    public int getStatusBarHeight() {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }


    private float downX;
    private float downY;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isDrag = false;
                    downX = event.getX();
                    downY = event.getY();

                    // 保证子View能够接收到Action_move事件
                    getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_MOVE:


                    Log.e("kid", "ACTION_MOVE");
                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;

                    int left_, right_, top_, bottom_;
                    //当水平或者垂直滑动距离大于10,才算拖动事件
                    if (Math.abs(xDistance) > 10 || Math.abs(yDistance) > 10) {
                        Log.e("kid", "Drag");
                        isDrag = true;
                        left_ = (int) (getLeft() + xDistance);
                        right_ = left_ + width;
                        top_ = (int) (getTop() + yDistance);
                        bottom_ = top_ + height;
                        //不划出边界判断,此处应按照项目实际情况,因为本项目需求移动的位置是手机全屏,
                        // 所以才能这么写,如果是固定区域,要得到父控件的宽高位置后再做处理
                        if (left_ < 0) {
                            left_ = 0;
                            right_ = left_ + width;


                        } else if (right_ > screenWidth) {
                            right_ = screenWidth;
                            left_ = right_ - width;
                        }
                        if (top_ < 0) {
                            top_ = 0;
                            bottom_ = top_ + height;
                        } else if (bottom_ > screenHeight) {
                            bottom_ = screenHeight;
                            top_ = bottom_ - height;
                        }
                        this.layout(left_, top_, right_, bottom_);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    setPressed(false);
                    //计算边界吸附效果

                    int LastX = getLeft();
                    int LastY = getTop();
                    L.v(TAG, "x:"+LastX +" y"+ LastY);
                    //计算一半的高度
                    int harfVerGrid = width / 2;
                    //计算第几行数据
                    int yNum = LastY / harfVerGrid;//多少行
                    int yLast = LastY % harfVerGrid;
                    if (yLast != 0) {
                        yNum = yNum + 1;
                    }
                    //计算吸附坐标
                    int MoveX=0;
                    int MoveY;
                    if (yLast < harfVerGrid / 2) {
                        MoveY = (yNum-1) * harfVerGrid;
                    } else {
                        MoveY = yNum * harfVerGrid;
                    }
                    //计算x坐标
                    int xNum = LastX / width;//多少列
                    int xLast = LastX % width;
                    if (xLast != 0) {
                        xNum = xNum + 1;
                    }
                    if (xLast < harfVerGrid) {
                        MoveX = xNum * width;
                    } else {
                        MoveX = (xNum+1) * width;
                    }

                    this.layout(MoveX, MoveY, width + MoveX, MoveY + height);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    setPressed(false);

                    break;
            }
            return true;
        }
        return false;
    }

    public interface PaintPositionListener {
        public void getPointPosition(int left, int top, int right, int bottom);
    }
}
